<template>
  
  <main>
    <div class="form-contianer">
      <div class="bg"></div>
      <div class="form">
      <LoginFormTitle v-show="getShow" class="enter-x" />
      <Form
      :model="formData"
      :rules="getFormRules"
      ref="formRef"
      v-show="getShow"
      @keypress.enter="handleLogin"
      >
        <FormItem name="account">
          <Input
          size="large"
          v-model:value="formData.account"
          :placeholder="t('sys.login.userName')"
          class="fix-auto-fill"
          >
          <template #prefix><UserOutlined style="color: rgba(0, 0, 0, 0.25);margin-right: 6px;" /></template>
          </Input>
        </FormItem>
        <FormItem name="password">
          <InputPassword
            size="large"
            visibilityToggle
            v-model:value="formData.password"
            accessKey="p"
            :placeholder="t('sys.login.password')"
          >
          <template #prefix><LockOutlined style="color: rgba(0, 0, 0, 0.25);margin-right: 6px;" /></template>
          </InputPassword>
        </FormItem>

        <ARow>
          <ACol :span="12">
            <FormItem>
              <!-- No logic, you need to deal with it yourself -->
              <Checkbox v-model:checked="rememberMeRef" size="medium">
                {{ t('sys.login.rememberMe') }}
              </Checkbox>
            </FormItem>
          </ACol>
        </ARow>

        <FormItem class="enter-x">
          <Button type="primary" size="large" shape='round' block @click="handleLogin" :loading="loading">
            {{ t('sys.login.loginButton') }}
          </Button>
        </FormItem>
        <Divider class="enter-x">{{ t('sys.login.otherSignIn') }}</Divider>
      </Form>
    </div>
    </div>
  </main>
</template>
<script lang="ts" setup>
import { LockOutlined, UserOutlined } from '@ant-design/icons-vue';
import { Button, Checkbox, Col, Divider, Form, Input, Row } from 'ant-design-vue';
import { computed, onMounted, reactive, ref, unref } from 'vue';
import LoginFormTitle from './LoginFormTitle.vue';
import { LoginStateEnum, useFormRules, useFormValid, useLoginState } from './useLogin';
import { useDesign } from '/@/hooks/web/useDesign';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { useUserStore } from '/@/store/modules/user';
  //import { onKeyStroke } from '@vueuse/core';

  const ACol = Col;
  const ARow = Row;
  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const { notification, createErrorModal } = useMessage();
  const { prefixCls } = useDesign('login');
  const userStore = useUserStore();

  const { setLoginState, getLoginState } = useLoginState();
  const { getFormRules } = useFormRules();

  const formRef = ref();
  const loading = ref(false);
  const rememberMeRef = ref(false);

  const formData = reactive({
    // account: 'root',
    // password: '123456',
    account: '',
    password: '',
  });

  const { validForm } = useFormValid(formRef);

  //onKeyStroke('Enter', handleLogin);

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN);

  async function handleLogin() {
    const data = await validForm();
    if (!data) return;
    try {
      if (rememberMeRef.value) {
        localStorage.setItem('rememberMe', JSON.stringify({rememberMe: true, account: data.account}))
      } else {
        localStorage.removeItem('rememberMe')
      }
      loading.value = true;
      const userInfo = await userStore.login({
        password: data.password,
        username: data.account,
        // mode: 'modal', //不要默认的错误提示
      });
      
      if (userInfo) {
        notification.success({
          message: t('sys.login.loginSuccessTitle'),
          description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.name}`,
          duration: 2,
        });
      }
    } catch (error) {
      // createErrorModal({
      //   title: t('sys.api.errorTip'),
      //   content: (error as unknown as Error).message || t('sys.api.networkExceptionMsg'),
      //   getContainer: () => document.body.querySelector(`.${prefixCls}`) || document.body,
      // });
    } finally {
      loading.value = false;
    }
  }

  onMounted(() => {
    try {
      let obj = JSON.parse(localStorage.getItem('rememberMe')||'')
      formData.account = obj.account
      rememberMeRef.value = obj.rememberMe
    } catch(err) {
    }
  })
</script>
<style scoped>
main{
  width: 100%;
  height: 100%;

}
main::before{
  content: '';
  background-image: url('../../../assets/images/a02.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  position: absolute;
  width: 100%;
  height: 100%;
  display: inline-block;
}
.bg{
  position: absolute;
  width: 100%;
  height: 100%;
  -webkit-backdrop-filter: blur(0px);
  backdrop-filter: blur(0px);
}

.form-contianer{
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.form{
  width: clamp(420px, 100%, 470px);
  background: rgb(255,255,255,.98);
  /* background: #fff; */
  border-radius: .8rem;
  padding: 50px;
  z-index: 99;
  border-color: transparent;
  box-sizing: border-box;
}
.form:hover{
  box-shadow: 10px 10px 10px -10px #c4c2c229,
              -10px 10px 10px #d4d1d11f,
              0px 10px 10px 10px #d5d2d217,
              0px -13px 10px 10px #c0bebe17;
  transition: box-shadow .4s,border-color .3s;
}
.ant-checkbox-wrapper, .ant-divider{
  -webkit-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
  color: grey;
}
</style>
